home *** CD-ROM | disk | FTP | other *** search
/ Cracking 2 / Cracking II..iso / Texty / crackme / TESTCRK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-14  |  14.8 KB  |  545 lines

  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <mem.h>
  5.  
  6. #define KEYCHARS 16
  7. #define KEYBITS KEYCHARS * 8
  8. #define XORCHARS 122
  9. #define XORBITS XORCHARS * 8
  10. #define EQUATIONS XORBITS + 32
  11. #define KEYSIZE KEYBITS * (KEYBITS + 1)
  12. #define XORSIZE XORBITS * (KEYBITS + 1)
  13.  
  14. char huge TabKey[KEYBITS][KEYBITS+1];         // password
  15. char huge TabCipher[EQUATIONS][KEYBITS+1];    // table for XOR
  16. char AktKey[32][KEYBITS+1];                   // current key
  17. char Temp[2][KEYBITS+1];                      // temporary for ROL
  18. char TabPass[KEYBITS];                        // password divided into bits
  19. char password[KEYCHARS];
  20. char TempEqu[12 * 8 + 1];                     // temp for swap_equ
  21. unsigned char solved[EQUATIONS];
  22.  
  23. FILE *org;
  24. char passsize;
  25. char size_pass[] = {11, 14, 14, 14};
  26. char passchars = 0, passbits = 0;
  27.  
  28. void do_test(void);
  29. void do_xor(char huge *, char huge *);
  30. void do_xor_equ(int, int);
  31. void swap_equ(int, int);
  32. char compute_char(int);
  33. unsigned long compute_eax(void);
  34. void create_table(void);
  35. int create_equations(void);
  36. int solve(void);
  37. int check_equation(int, int);
  38. void show_pass(int);
  39. void insert_values(void);
  40. int search(int, int);
  41. void set_eax(void);
  42.  
  43. main()
  44. {
  45.   cputs("\r\n╔══════════════════════════════════════════════════════════════════════════════╗");
  46.   cputs("║                          TEST4 from LordByte SOLVED                          ║");
  47.   cputs("║                 All the hard work was done in 1997 by CR00CK                 ║");
  48.   cputs("║     The only purpose of this program is to show how bad is this routine      ║");
  49.   cputs("║                                used in TEST4                                 ║");
  50.   cputs("║                               ONE-TIME PAD ?!?                               ║");
  51.   cputs("║                           Forget about it!!!!!!!!                            ║");
  52.   cputs("╚══════════════════════════════════════════════════════════════════════════════╝");
  53.   do {
  54.     cputs("How many chars (1 - 12): ");
  55.     scanf("%i", &passchars);
  56.   } while (passchars < 1 || passchars > 12);
  57.   passbits = 8 * passchars;
  58.   cputs("Wait for a while ...");
  59.  
  60.   create_table();
  61.   do_test();
  62.   create_equations();
  63.   solve();
  64.  
  65.   return 0;
  66. }
  67.  
  68. void create_table()
  69. {
  70.   int i, j;
  71.   unsigned char mask, chr, byte[XORCHARS];
  72.   long cipherpos;
  73.   int pass, keychar, bits, xor_char;
  74.   unsigned char buffer[KEYBITS + 1];
  75.  
  76.   if ((org = fopen("CIPHER.DAT", "rb")) != NULL)
  77.   {
  78.     fread(byte, 1, 1, org);
  79.     if (byte[0] == passchars)
  80.     {
  81.       printf("reading from CIPHER.DAT...\n");
  82.       for (i = 0; i < EQUATIONS; i++)
  83.       {
  84.         fread(buffer, KEYBITS+1, 1, org);
  85.         // we cannot use _fmemcpy, coz it fails sometimes on huge tables
  86.         for (j = 0; j <= KEYBITS; j++)
  87.           TabCipher[i][j] = buffer[j];
  88.       }
  89.       fclose(org);
  90.       return;
  91.     }
  92.   }
  93.  
  94.   memset(TabPass, 0, KEYBITS);
  95.   _fmemset(TabKey, 0, KEYSIZE);   // initiate TabKey
  96.   for (i = 0; i < passbits; i++)  // set passbits bits
  97.     TabKey[i][i] = 1;
  98.   TabKey[passchars*8][KEYBITS] = 0;   // char after password is 0x0d
  99.   TabKey[passchars*8+1][KEYBITS] = 0;
  100.   TabKey[passchars*8+2][KEYBITS] = 0;
  101.   TabKey[passchars*8+3][KEYBITS] = 0;
  102.   TabKey[passchars*8+4][KEYBITS] = 1;
  103.   TabKey[passchars*8+5][KEYBITS] = 1;
  104.   TabKey[passchars*8+6][KEYBITS] = 0;
  105.   TabKey[passchars*8+7][KEYBITS] = 1;
  106.   TabKey[13*8][KEYBITS] = 0;         // 13th char is 0x0a
  107.   TabKey[13*8+1][KEYBITS] = 0;
  108.   TabKey[13*8+2][KEYBITS] = 0;
  109.   TabKey[13*8+3][KEYBITS] = 0;
  110.   TabKey[13*8+4][KEYBITS] = 1;
  111.   TabKey[13*8+5][KEYBITS] = 0;
  112.   TabKey[13*8+6][KEYBITS] = 1;
  113.   TabKey[13*8+7][KEYBITS] = 0;
  114.   TabKey[14*8][KEYBITS] = 0;         // on 14th pos we've got 0x0d
  115.   TabKey[14*8+1][KEYBITS] = 0;
  116.   TabKey[14*8+2][KEYBITS] = 0;
  117.   TabKey[14*8+3][KEYBITS] = 0;
  118.   TabKey[14*8+4][KEYBITS] = 1;
  119.   TabKey[14*8+5][KEYBITS] = 1;
  120.   TabKey[14*8+6][KEYBITS] = 0;
  121.   TabKey[14*8+7][KEYBITS] = 1;
  122.   TabKey[15*8][KEYBITS] = 0;         // on 15th there's is 0x20
  123.   TabKey[15*8+1][KEYBITS] = 0;
  124.   TabKey[15*8+2][KEYBITS] = 1;
  125.   TabKey[15*8+3][KEYBITS] = 0;
  126.   TabKey[15*8+4][KEYBITS] = 0;
  127.   TabKey[15*8+5][KEYBITS] = 0;
  128.   TabKey[15*8+6][KEYBITS] = 0;
  129.   TabKey[15*8+7][KEYBITS] = 0;
  130.   passsize = passchars;
  131.  
  132.   if ((org = fopen("ORG.DAT", "rb")) == NULL)
  133.   {
  134.     printf("ORG.DAT not found!\n");
  135.     exit(255);
  136.   }
  137.  
  138.   _fmemset(TabCipher, 0, XORSIZE);  // initiate TabCipher
  139.   cipherpos = 0;
  140.   fread(byte, XORCHARS, 1, org);
  141.   fclose(org);
  142.   for (i = 0; i < XORCHARS; i++)
  143.     for (mask = 0x80; mask != 0; mask >>= 1)
  144.     {
  145.       if ((byte[i] & mask) == mask) TabCipher[cipherpos][KEYBITS] = 1;
  146.       cipherpos++;
  147.     }
  148.  
  149.   for (pass = 0; pass < 5; pass++)
  150.   {
  151.     for (keychar = 0; keychar < (passsize - 1); keychar++)
  152.     {
  153.       putch('.');
  154.       memcpy(AktKey, TabKey[keychar*8+24], 8*(KEYBITS+1)); // get current key
  155.       memcpy(AktKey[8], TabKey[keychar*8+16], 8*(KEYBITS+1)); // but reversed
  156.       memcpy(AktKey[16], TabKey[keychar*8+8], 8*(KEYBITS+1));
  157.       memcpy(AktKey[24], TabKey[keychar*8], 8*(KEYBITS+1));
  158.       for (xor_char = 0; xor_char <= (XORCHARS - 4); xor_char++)
  159.       {
  160.         for (bits = 24; bits < 32; bits++)
  161.           do_xor(TabCipher[xor_char * 8 + bits - 24], AktKey[bits]);
  162.         for (bits = 16; bits < 24; bits++)
  163.           do_xor(TabCipher[xor_char * 8 + bits - 8], AktKey[bits]);
  164.         for (bits = 8; bits < 16; bits++)
  165.           do_xor(TabCipher[xor_char * 8 + bits + 8], AktKey[bits]);
  166.         for (bits = 0; bits < 8; bits++)
  167.           do_xor(TabCipher[xor_char * 8 + bits + 24], AktKey[bits]);
  168.         memcpy(Temp, AktKey, 2 * (KEYBITS + 1));
  169.         memmove(AktKey, AktKey[2], 30 * (KEYBITS + 1));
  170.         memcpy(AktKey[30], Temp, 2 * (KEYBITS + 1));
  171.       }
  172.     }
  173.     for (bits = 0; bits < 32; bits++)
  174.     {
  175.       do_xor(TabKey[bits], TabCipher[bits]);
  176.       do_xor(TabKey[32 + bits], TabCipher[bits]);
  177.       do_xor(TabKey[64 + bits], TabCipher[bits]);
  178.     }
  179.     if (pass == 2) set_eax();
  180.     if (passsize < 12) passsize = 14;
  181.   }
  182.  
  183.   if ((org = fopen("CIPHER.DAT", "wb")) != NULL)
  184.   {
  185.     byte[0] = passchars;
  186.     fwrite(byte, 1, 1, org);
  187.     for (i = 0; i < EQUATIONS; i++)
  188.     {
  189.       for (j = 0; j <= KEYBITS; j++)
  190.       // we cannot simply memcpy, coz it doesn't work properly on huge tables
  191.         buffer[j] = TabCipher[i][j];
  192.       fwrite(buffer, KEYBITS+1, 1, org);
  193.     }
  194.     fclose(org);
  195.   }
  196. }
  197.  
  198. void set_eax()
  199. {
  200.   int i;
  201.  
  202.   for (i = 0; i < 32; i++)
  203.   {
  204.     _fmemcpy(TabCipher[XORBITS + i], TabCipher[i], KEYBITS+1);
  205.     _fmemset(TabCipher[i], 0, KEYBITS+1);
  206.   }
  207.   /* this is binary encoded 94, 09, 61, da, all hex */
  208.   TabCipher[0][KEYBITS] = 1;
  209.   TabCipher[1][KEYBITS] = 0;
  210.   TabCipher[2][KEYBITS] = 0;
  211.   TabCipher[3][KEYBITS] = 1;
  212.   TabCipher[4][KEYBITS] = 0;
  213.   TabCipher[5][KEYBITS] = 1;
  214.   TabCipher[6][KEYBITS] = 0;
  215.   TabCipher[7][KEYBITS] = 0;
  216.   TabCipher[8][KEYBITS] = 0;
  217.   TabCipher[9][KEYBITS] = 0;
  218.   TabCipher[10][KEYBITS] = 0;
  219.   TabCipher[11][KEYBITS] = 0;
  220.   TabCipher[12][KEYBITS] = 1;
  221.   TabCipher[13][KEYBITS] = 0;
  222.   TabCipher[14][KEYBITS] = 0;
  223.   TabCipher[15][KEYBITS] = 1;
  224.   TabCipher[16][KEYBITS] = 0;
  225.   TabCipher[17][KEYBITS] = 1;
  226.   TabCipher[18][KEYBITS] = 1;
  227.   TabCipher[19][KEYBITS] = 0;
  228.   TabCipher[20][KEYBITS] = 0;
  229.   TabCipher[21][KEYBITS] = 0;
  230.   TabCipher[22][KEYBITS] = 0;
  231.   TabCipher[23][KEYBITS] = 1;
  232.   TabCipher[24][KEYBITS] = 1;
  233.   TabCipher[25][KEYBITS] = 1;
  234.   TabCipher[26][KEYBITS] = 0;
  235.   TabCipher[27][KEYBITS] = 1;
  236.   TabCipher[28][KEYBITS] = 1;
  237.   TabCipher[29][KEYBITS] = 0;
  238.   TabCipher[30][KEYBITS] = 1;
  239.   TabCipher[31][KEYBITS] = 0;
  240. }
  241.  
  242. void do_test()
  243. {
  244.   int i, mask, bits = 0;
  245.   unsigned char test[123];
  246.  
  247.   memset(password, 0, KEYCHARS);
  248.   printf("\nEnter password to test (%i chars): ", passchars);
  249.   scanf("%12s", password);
  250.   password[passchars]=0xd;
  251.   password[13]=0xa;
  252.   password[14]=0xd;
  253.   password[15]=0x20;
  254.  
  255.   memset(TabPass, 0, KEYBITS);
  256.   for (i = 0; i < KEYCHARS; i++)
  257.     for (mask = 0x80; mask != 0; mask >>= 1)
  258.     {
  259.       if ((password[i] & mask) == mask) TabPass[bits] = 1;
  260.       bits++;
  261.     }
  262.   for (i = 0; i < XORCHARS; i++)
  263.     test[i] = compute_char(i);
  264.   if ((org = fopen("OUT.DAT", "wb")) != NULL)
  265.   {
  266.     fwrite(test, XORCHARS, 1, org);
  267.     fclose(org);
  268.   }
  269. }
  270.  
  271. void do_xor(char huge *dest, char huge *source)
  272. {
  273. /*  int count;
  274.  
  275.   for (count = 0; count <= KEYBITS; count++)
  276.     dest[count] ^= source[count];*/
  277.   asm{
  278.     push  ds
  279.     push  es
  280.     lds   ax,dest
  281.     mov   bx,ds
  282.     mov   dx,ax
  283.     and   ax,15
  284.     mov   si,ax
  285.     shr   dx,4
  286.     add   bx,dx
  287.     mov   ds,bx // normalized (offset < 0x10) dest to ds:si
  288.     les   ax,source
  289.     mov   bx,es
  290.     mov   dx,ax
  291.     and   ax,15
  292.     mov   di,ax
  293.     shr   dx,4
  294.     add   bx,dx
  295.     mov   es,bx // normalized source to es:di
  296.     mov   cx,KEYBITS + 1
  297.   }
  298.   petla:
  299.   asm {
  300.     mov   al,es:[di]
  301.     xor   ds:[si],al
  302.     inc   di
  303.     inc   si
  304.     loop  petla
  305.     pop   es
  306.     pop   ds
  307.   }
  308. }
  309.  
  310. char compute_char(int offset)
  311. {
  312.   int result = 0, i, bit, mask, bits = 0;
  313.  
  314.   for (mask = 0x80; mask != 0; mask >>= 1)        // process all 8 bits
  315.   {
  316.     bit = TabCipher[offset*8+bits][KEYBITS];      // get 1 value (if 1 is set)
  317.     for (i = 0; i < KEYBITS; i++)                 // process all others
  318.       if (TabCipher[offset*8+bits][i]) bit ^= TabPass[i]; // XOR with password bit
  319.     if (bit) result |= mask;
  320.     bits++;
  321.   }
  322.  
  323.   return result;
  324. }
  325.  
  326. unsigned long compute_eax(void)
  327. {
  328.   int bits = 0, i, bit;
  329.   unsigned long mask = 0x80000000, result = 0;
  330.  
  331.   while (mask != 0)
  332.   {
  333.     bit = AktKey[bits][KEYBITS];
  334.     for (i = 0; i < KEYBITS; i++)
  335.       if (AktKey[bits][i]) bit ^= TabPass[i];
  336.     if (bit) result |= mask;
  337.     bits++;
  338.     mask >>= 1;
  339.   }
  340.   return result;
  341. }
  342.  
  343. create_equations(void)
  344. {
  345.   int i, targetpos = 0;
  346.   unsigned char byte[XORCHARS], mask;
  347.  
  348.   // now, the TabCipher becomes a table where equations to solve are stored
  349.   // first passbits elements are equations itselves, next one is a
  350.   // free word (value on the right side of equation)
  351.  
  352.   if ((org = fopen("XOR.DAT", "rb")) == NULL)
  353.   {
  354.     printf("XOR.DAT not found!\n");
  355.     exit(255);
  356.   }
  357.  
  358.   fread(byte, XORCHARS, 1, org);
  359.   fclose(org);
  360.   for (i = 0; i < XORCHARS; i++)
  361.     for (mask = 0x80; mask != 0; mask >>= 1)
  362.     {
  363.       if ((byte[i] & mask) == mask)
  364.         TabCipher[targetpos][passbits] = 1;
  365.       else
  366.         TabCipher[targetpos][passbits] = 0;
  367.       targetpos++;
  368.     }
  369.   TabCipher[XORBITS][passbits] = 1;
  370.   TabCipher[XORBITS+1][passbits] = 0;
  371.   TabCipher[XORBITS+2][passbits] = 0;
  372.   TabCipher[XORBITS+3][passbits] = 1;
  373.   TabCipher[XORBITS+4][passbits] = 0;
  374.   TabCipher[XORBITS+5][passbits] = 1;
  375.   TabCipher[XORBITS+6][passbits] = 0;
  376.   TabCipher[XORBITS+7][passbits] = 0;
  377.   TabCipher[XORBITS+8][passbits] = 0;
  378.   TabCipher[XORBITS+9][passbits] = 0;
  379.   TabCipher[XORBITS+10][passbits] = 0;
  380.   TabCipher[XORBITS+11][passbits] = 0;
  381.   TabCipher[XORBITS+12][passbits] = 1;
  382.   TabCipher[XORBITS+13][passbits] = 0;
  383.   TabCipher[XORBITS+14][passbits] = 0;
  384.   TabCipher[XORBITS+15][passbits] = 1;
  385.   TabCipher[XORBITS+16][passbits] = 0;
  386.   TabCipher[XORBITS+17][passbits] = 1;
  387.   TabCipher[XORBITS+18][passbits] = 1;
  388.   TabCipher[XORBITS+19][passbits] = 0;
  389.   TabCipher[XORBITS+20][passbits] = 0;
  390.   TabCipher[XORBITS+21][passbits] = 0;
  391.   TabCipher[XORBITS+22][passbits] = 0;
  392.   TabCipher[XORBITS+23][passbits] = 1;
  393.   TabCipher[XORBITS+24][passbits] = 1;
  394.   TabCipher[XORBITS+25][passbits] = 1;
  395.   TabCipher[XORBITS+26][passbits] = 0;
  396.   TabCipher[XORBITS+27][passbits] = 1;
  397.   TabCipher[XORBITS+28][passbits] = 1;
  398.   TabCipher[XORBITS+29][passbits] = 0;
  399.   TabCipher[XORBITS+30][passbits] = 1;
  400.   TabCipher[XORBITS+31][passbits] = 0;
  401.  
  402.   printf("\n");
  403.   for (i = 0; i < EQUATIONS; i++)   // if there's one on the left side of the equation
  404.     if (TabCipher[i][KEYBITS])
  405.       TabCipher[i][passbits] ^= 1; // then get rid of it
  406.  
  407.   return 0;
  408. }
  409.  
  410. void do_xor_equ(int num1, int num2)
  411. {
  412.   int i;
  413.  
  414.   for (i = 0; i <= passbits; i++)
  415.     TabCipher[num1][i] ^= TabCipher[num2][i];
  416. }
  417.  
  418. void swap_equ(int num1, int num2)
  419. {
  420.   memcpy(TempEqu, TabCipher[num1], passbits + 1);
  421.   memcpy(TabCipher[num1], TabCipher[num2], passbits + 1);
  422.   memcpy(TabCipher[num2], TempEqu, passbits + 1);
  423. }
  424.  
  425. solve()
  426. {
  427.   int i, j, k, jest, test;
  428.   unsigned char buffer[12 * 8 + 1];
  429.  
  430.   memset(solved, 0, EQUATIONS);
  431.   printf("Solving set of %i equations. This may take while...\n", EQUATIONS);
  432.   for (i = passbits - 1; i >= 0; i--)
  433.   {
  434.     printf("\rProcessing bit %i (out of %i) ", i + 1, passbits);
  435.     jest = 0;
  436.     for (k = 0; k < EQUATIONS; k++)
  437.       if (TabCipher[k][i] && !solved[k])
  438.       {
  439.         jest = 1;
  440.         break;
  441.       }
  442.     if (!jest)
  443.     {
  444.       printf("\nThere is no entry for bit %i\n", i+1);
  445.       continue;
  446.     }
  447.     else
  448.     {
  449.       solved[k] = 1;
  450.       for (j = 0; j < EQUATIONS; j++)
  451.         if ((j != k) && (TabCipher[j][i] == 1))
  452.           do_xor_equ(j, k);
  453.     }
  454.   }
  455.   if ((org = fopen("EQU.DAT", "wb")) != NULL)
  456.   {
  457.     for (i = 0; i < EQUATIONS; i++)
  458.     {
  459.       for (j = 0; j <= passbits; j++)
  460.       // guess, why we aren't using memcpy
  461.         buffer[j] = TabCipher[i][j];
  462.       fwrite(buffer, passbits+1, 1, org);
  463.     }
  464.     fclose(org);
  465.   }
  466.   printf("\nSuccess!!!\n");
  467.   insert_values();
  468.   show_pass(i);
  469.   return 0;
  470. }
  471.  
  472. void insert_values()
  473. {
  474.   int i, j;
  475.   unsigned char bit, bits, have_value[12 * 8], values[12 * 8];
  476.  
  477.   memset(have_value, 0, passbits);
  478.   memset(values, 0, passbits);
  479.   for (i = 0; i < EQUATIONS; i++)
  480.   {
  481.     bits = 0;
  482.     for (j = 0; j < passbits; j++)
  483.       if (TabCipher[i][j])
  484.       {
  485.         bits++;
  486.         bit = j;
  487.       }
  488.     if (bits == 1)
  489.     {
  490.       if (have_value[bit] && values[bit] != TabCipher[i][passbits])
  491.       {
  492.         printf("This set of equations is contradictory!\n");
  493.         return;
  494.       }
  495.       have_value[bit] = 1;
  496.       TabPass[bit] = values[bit] = TabCipher[i][passbits];
  497.     }
  498.   }
  499.   for (i = 0; i < passbits; i++)
  500.   {
  501.     if (!have_value[i]) continue;
  502.     for (j = 0; j < EQUATIONS; j++)
  503.       if (TabCipher[j][i])
  504.       {
  505.         TabCipher[j][i] = 0;
  506.         TabCipher[j][passbits] ^= values[i];
  507.       }
  508.   }
  509. }
  510.  
  511. #pragma argsused
  512. void show_pass(int how_many)
  513. {
  514.   int bits, i, bitcount;
  515.   unsigned char mask = 0x80, c = 0;
  516.   FILE *passout;
  517.  
  518.   if ((passout = fopen("TEST.OUT", "aw")) != NULL)
  519.   {
  520.     fprintf(passout, "Password chars: %i\n", passchars);
  521.     fprintf(passout, "Sure password chars:\n");
  522.     for (bits = 0; bits < passbits; bits++)
  523.     {
  524.       if (TabPass[bits]) c |= mask;
  525.       mask >>= 1;
  526.       if (mask == 0)
  527.       {
  528.         fprintf(passout, "(%c) - %i\n", c, c);
  529.         mask = 0x80;
  530.         c = 0;
  531.       }
  532.     }
  533.     fclose(passout);
  534.   }
  535. }
  536.  
  537. int search(int bit1, int bit2)
  538. {
  539.   int i;
  540.  
  541.   for (i = 0; i < EQUATIONS; i++)
  542.     if (TabCipher[i][bit1] != TabCipher[i][bit2])
  543.       return i;
  544.   return 0;
  545. }